/**
 * @author amalafe
 */

Namespace("sketch.framework");
(function($){
	//usings
	var fwk = sketch.framework;
	fwk.EventHelper = {
		preventDefault : function(event){
			if(event.preventDefault)
			{		
				event.preventDefault();
			}
			else
			{ 
				event.returnValue = false;
			}
		},
		eventToViewPoint : function(event){
			var x = event.layerX,
				y = event.layerY,
				target = event.target,
				parent = $(target).parents('[data-type="view"]')[0];
			
			if(parent === null){
				throw {
					name:"InvalidOperation",
					description:"Element doesn't belongs to view"
				};
			}
			 
			while(target !== null && target !== parent){
				x += target.offsetLeft;
				y += target.offsetTop;
				
				target = target.parentNode;
			}
			
			return {x:x, y:y};
		},
        createEvent : function(){
            var func = function(){
                for (var i = func.subscribers.length - 1; i >= 0; --i) {
                    func.subscribers[i].apply(this, arguments);
                }
            };
            func.subscribers = [];
            func.bind = function(f){
                func.subscribers.push(f);
            };

            func.unbind = function(f){
                for(var i = func.subscribers.length - 1; i>=0; --i){
                    if(func.subscribers[i] === f){
                        func.subscribers.splice(i, 1);
                        return;
                    }
                }
            }

            return func;
        }
	};
})(jQuery);
